package cn.com.cym.gamezao.base;

import cn.com.cym.gamezao.annotation.AuthToken;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.NonNull;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * controller 基类
 */
@AuthToken
public abstract class BaseController<T extends BaseEntity> {

    /**
     * 获取当前的service
     * @return BaseService
     */
    public abstract BaseService<T> getService();


    @ApiImplicitParam(name = "id", value = "id", required = true,dataType = "Long")
    @ApiOperation(value = "按id获取")
    @ApiOperationSupport(order = 1, author = "mr.chen")
    @GetMapping("/{id}")
    @AuthToken(rule = "search")
    public ResponseEntity<?> getById(@PathVariable @NonNull Long id){
        T t = this.getService().fetchOne(id);
        return ResponseEntity.ok(t);
    }


    @ApiImplicitParam(name = "id", value = "id",paramType = "path",required = true,dataType = "Long")
    @ApiOperation(value = "删除实体")
    @ApiOperationSupport(order = 2, author = "mr.chen")
    @DeleteMapping("/{id}")
    @AuthToken(rule = "delete")
    public ResponseEntity<?> delete(@PathVariable Long id){
        this.getService().physicalDelete(id);
        return ResponseEntity.ok(new ResultResponse("删除成功", HttpStatus.OK.value()));
    }

    @ApiImplicitParam(name = "searchPage", value = "searchPage", required = true,dataType = "SearchPage")
    @ApiOperation(value = "分页显示page！")
    @ApiOperationSupport(order = 4, author = "mr.chen")
    @PostMapping("/page")
    @AuthToken(rule = "search")
    public ResponseEntity<?> page(@RequestBody SearchPage searchPage){
        Page<T> byPageable = this.getService().findByPageable(searchPage);
        return ResponseEntity.ok(byPageable);
    }

    @ApiImplicitParam(name = "ids", value = "ids",required = true,dataType = "List")
    @ApiOperation(value = "删除实体")
    @ApiOperationSupport(order = 2, author = "mr.chen")
    @DeleteMapping("/")
    @AuthToken(rule = "delete")
    public ResponseEntity<?> deleteByIdS(@RequestBody List<Long> ids){
        this.getService().physicalDeleteByIds(ids);
        return ResponseEntity.ok(new ResultResponse("批量删除成功", HttpStatus.OK.value()));
    }
}
